package pt.compflow.client.api;

import org.json.JSONArray;
import org.json.JSONObject;

import pt.compflow.client.BaseResource;
import pt.compflow.flowontology.model.FlowOntologyModel;
import pt.compflow.flowontology.model.Person;
import pt.compflow.flowontology.model.PersonGroupMembership;
import pt.compflow.flowontology.model.TaskInterface;

import com.hp.hpl.jena.util.iterator.ExtendedIterator;

public abstract class APIBaseResource extends BaseResource {

	protected JSONObject getPersonJson(FlowOntologyModel model, Person person) {
		JSONObject obj = new JSONObject();
		
		obj.put("id", person.getURI());
		obj.put("firstName", person.getHasFirstName(null));
		obj.put("lastName", person.getHasLastName(null));
		obj.put("email", person.getHasEmail(null));
		obj.put("dateOfBirth", person.getHasDateOfBirth());
		
		JSONArray membershipList = new JSONArray();
		ExtendedIterator<PersonGroupMembership> it = person.listHasMembershipPersonGroupMemberships();
		while(it.hasNext()) {
			PersonGroupMembership membership = it.next();
			JSONObject membershipMap = new JSONObject();
			membershipMap.put("uri", membership.getHasGroupPersonGroup().getURI());
			membershipMap.put("label", membership.getHasGroupPersonGroup().getLabel(null));
			membershipList.put(membershipMap);
		}
		obj.put("memberships", membershipList);
		
		// worker stuff		
		JSONArray interfaceList = new JSONArray();
		ExtendedIterator<TaskInterface> it2 = person.listAccessibleThroughTaskInterfaces();
		while(it2.hasNext()) {
			TaskInterface interfaze = it2.next();
			JSONObject iMap = new JSONObject();
			iMap.put("uri", interfaze.getURI());
			iMap.put("label", interfaze.getLabel(null));
			interfaceList.put(iMap);
		}
		obj.put("interfaces", interfaceList);
		
		return obj;
	}
}
